//
//  MessageFrame.m
//  11-QQ聊天
//
//  Created by wangneihan on 15/12/22.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#import "MessageFrame.h"
#import "Message.h"

@implementation MessageFrame

-(void)setMessage:(Message *)message
{
    _message = message;
    
    //根据内容，生成控件Frame
    UIScreen *screen = [UIScreen mainScreen];
    //1. 时间控件
    CGFloat margin = 10;
    CGFloat timeW = screen.bounds.size.width;
    CGFloat timeH = 40;
    CGFloat timeX = 0;
    CGFloat timeY = 0;
    if(!message.isHiddenTime)
        self.timeLabelFrame = CGRectMake(timeX, timeY, timeW, timeH);
    
    //2. 头像
    CGFloat iconW = 50;
    CGFloat iconH = 50;
    CGFloat iconX = 0;
    CGFloat iconY = CGRectGetMaxY(_timeLabelFrame);
    if(message.type == MessageTypeSelf)
    {
        iconX = screen.bounds.size.width - iconW - margin;
    }
    else
    {
        iconX = margin;
    }
    _iconViewlFrame = CGRectMake(iconX, iconY, iconW, iconH);
    
    //2. 聊天内容
    //使用Btn 有最大宽度，但高度不固定。
    CGSize textSize = [self sizeWithMaxSize:CGSizeMake(200, MAXFLOAT) fontSize:FontSize string:message.text];
    //self.msgButtonFrame.
    CGSize btnSize = CGSizeMake(textSize.width + 20 * 2, textSize.height + 20 * 2);
    
    CGFloat textX ;
    CGFloat textY = iconY;
    if(message.type == MessageTypeSelf)
    {
        textX = iconX - btnSize.width - margin;
    }
    else
    {
        textX = CGRectGetMaxX(_iconViewlFrame) + margin;
    }

    _msgButtonFrame = CGRectMake(textX, textY, btnSize.width, btnSize.height);
    
    CGFloat iconMaxY = CGRectGetMaxY(_iconViewlFrame);
    
    CGFloat textMaxY = CGRectGetMaxY(_msgButtonFrame);
    
    _rowHight = MAX(iconMaxY, textMaxY) + margin;
    
}

- (CGSize)sizeWithMaxSize:(CGSize)maxSize fontSize:(CGFloat)fontSize string:(NSString *)str
{
    return [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
}

@end
